package com.xiaokan.utils;

public class IntByteArrayConverter {

	public static byte[] int2bytearray(int num) {
		byte[] ret = new byte[4];
		for (int i = 0; i < 4; i++) {
			ret[i] = (byte) (num & 0xFF);
			num >>= 8;
		}
		return ret;
	}

	/**
	 * This method recovers the bytearray produced by the <i>int2bytearray()</i>
	 * to the integer
	 * 
	 * @param The
	 *            bytearray
	 * @return recovered integer
	 */
	public static int bytearray2int(byte ba[]) {
		int ret = 0;
		for (int i = 3; i >= 0; i--) {
			ret <<= 8;
			int t = ba[i];
			if (t < 0) {
				t = ~t;
				t ^= 0xFF;
			}
			ret += t;

		}
		return ret;

	}

}
